package com.wjw.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.LADD;
import com.wjw.entity.Category;
import com.wjw.entity.ProductPicture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.file.Watchable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjw.dao.ProductMapper;
import com.wjw.entity.Product;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author WuJianWei
 * @date 2022/2/28 20:00
 */
@Service
public class ProductService extends ServiceImpl<ProductMapper, Product> {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CategoryService categoryService;

    public Product getProductByID(Integer productId) {
        Map<Object, Object> productMap = redisTemplate.opsForHash().entries("product");
        //redis无map
        if (productMap.isEmpty()) {
            Product product = baseMapper.selectById(productId);
            productMap.put(productId + "", product);
            redisTemplate.opsForHash().putAll("product", productMap);
            //redis无该商品
        } else if (productMap.get(productId) == null) {
            productMap.put(productId + "", baseMapper.selectById(productId));
            redisTemplate.opsForHash().putAll("product", productMap);
        }
        return (Product) productMap.get(productId + "");
    }

    /**
     *
     * @return
     */
    public List<Product> getAllProduct(){
        Map<Object, Object> products = redisTemplate.opsForHash().entries("products");
        //redis中无数据，查询数据库
        if(null==products||products.size()==0){
            LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Product::getStatus,1);
            List<Product> allProduct = baseMapper.selectList(wrapper);
            products.put("products",allProduct);
            redisTemplate.opsForHash().putAll("products",products);
        }
        return (List<Product>) products.get("products");
    }

    /**
     *
     * @param categoryID
     * @param currentPage
     * @param pageSize
     * @return
     */
    public List<Product> getProductByCategory(int categoryID,int currentPage,int pageSize){
        Page<Product> page = new Page(currentPage,pageSize);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getCategoryId,categoryID);
        IPage<Product> productPage = baseMapper.selectPage(page, wrapper);
        List<Product> records = productPage.getRecords();
        return records;
    }

    public List<Product> getProductByCategoryName(String categoryName){
        Integer categoryId = categoryService.getProductByCategoryName(categoryName);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getCategoryId,categoryId);
        List<Product> products = baseMapper.selectList(wrapper);
        return products;
    }

    public List<Product> getProductBySearch(String search){
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Product::getProductName,search);
        wrapper.like(Product::getProductTitle,search);
        wrapper.like(Product::getProductIntro,search);
        List<Product> products = baseMapper.selectList(wrapper);
        return products;
    }


    public Boolean add(Product product){
        product.setStatus(1);
        int insert = baseMapper.insert(product);
        if (insert > 0){
            return true;
        }
        return false;
    }


    public List<Product> getProduct(Long businessId){
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getBusinessId,businessId);
        List<Product> products = baseMapper.selectList(wrapper);
        return products;
    }

    public int  delete(Integer productId){
        return baseMapper.deleteById(productId);

    }


}
